import {
  create,
  deleteJournalById,
  listAllByPage,
  selectAllJournalsByUser,
  queryAllProjects,
  update, selectAllDevelopers, listJournalsByPage,
} from '@/services/projectjournalApi';

export default {
  namespace: 'projectjournal',
  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    /**
     * 分页获取数据
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *listJournalsByPage({ payload }, { call, put }) {
      const response = yield call(listJournalsByPage, payload);
      yield put({
        type: 'cb4listAllByPage',
        payload: response,
      });
    },

    /**
     * 新增
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *create({ payload }, { call, put }) {
      const response = yield call(create, payload);
      return response;
    },

    /**
     * 获取所有的项目列表
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *queryAllProjects({ payload }, { call, put }) {
      const response = yield call(queryAllProjects, payload);
      return response;
    },

    /**
     * 获取所有的日志
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *selectAllJournalsByUser({ payload }, { call, put }) {
      const response = yield call(selectAllJournalsByUser, payload);
      return response;
    },

    /**
     * 获取所有的开发人员
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *selectAllDevelopers({ payload }, { call, put }) {
      const response = yield call(selectAllDevelopers, payload);
      return response;
    },

    /**
     * 更新
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *update({ payload }, { call, put }) {
      const response = yield call(update, payload);
      return response;
    },

    /**
     * 删除
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *deleteJournalById({ payload }, { call, put }) {
      const response = yield call(deleteJournalById, payload);
      return response;
    },
  },

  reducers: {
    cb4listAllByPage(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
  },
};
